//
//  StockManagePopView.m
//  TryShopping
//
//  Created by LiYinDi on 16/6/22.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "StockManagePopView.h"
#import "StockManagementPopCell.h"
#import "StockManagementPopModel.h"
@interface StockManagePopView()<UITableViewDataSource,UITableViewDelegate>
{
    UIButton *topAlphaBtn;
    UIButton *cancelBtn;
    UIView *parentView;
    UILabel *lineLabel;
    UILabel *productStockLabel;
    UILabel *productSpecLabel;
    UIImageView *productImageView;
    UILabel *productNameLabel;
    UITableView *listTableView;
    UILabel*noLabel;
    NSMutableArray *cellModelArray;
    UIView*noDataView;
}
@end
@implementation StockManagePopView
-(instancetype)init
{
    self=[super init];
    if (self) {
        cellModelArray=[NSMutableArray array];
        [self createUI];
        [self setAutoLayout];
    }
    return self;
}
-(void)createUI
{
    [noLabel removeFromSuperview];
    topAlphaBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [topAlphaBtn setBackgroundColor:[UIColor clearColor]];
    [topAlphaBtn addTarget:self action:@selector(topAlphaBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [topAlphaBtn setBackgroundColor: [Global parseColor:@"#66000000"]];
    [self addSubview:topAlphaBtn];
    
    parentView=[[UIView alloc] init];
    [parentView setBackgroundColor:[UIColor whiteColor]];
     [self addSubview:parentView];
    
    cancelBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setImage:[UIImage imageNamed:@"chas"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(topAlphaBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];

    
    productImageView=[[UIImageView alloc] init];
    productImageView.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1] CGColor];
    productImageView.layer.borderWidth= 0.5f;

    [parentView addSubview: productImageView];
    
    productNameLabel=[[UILabel alloc] init];
    productNameLabel.numberOfLines=2;
    productNameLabel.textColor=[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0];
    productNameLabel.font=[UIFont systemFontOfSize:14];
    [parentView addSubview: productNameLabel];
    
    productSpecLabel=[[UILabel alloc] init];
    productSpecLabel.textColor=[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0];
    productSpecLabel.font=[UIFont systemFontOfSize:12];
    [parentView addSubview: productSpecLabel];
    
    productStockLabel=[[UILabel alloc] init];
    productStockLabel.textColor=[UIColor colorWithRed:90.0f/255.0f green:130.0f/255.0f blue:26.0f/255.0f alpha:1.0];
    productStockLabel.font=[UIFont systemFontOfSize:12];
    [parentView addSubview: productStockLabel];
    
    
    lineLabel=[[UILabel alloc] init];
    lineLabel.backgroundColor=[UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0];
    
    [parentView addSubview: lineLabel];
    
    listTableView=[[UITableView alloc] init];
    [listTableView setDataSource:self];
    [listTableView setDelegate:self];
    listTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [parentView addSubview: listTableView];

    
}
-(void)topAlphaBtnClick
{
   [self removeFromSuperview];
}
- (void)show:(NSString *)specId
{
    // 获得主窗口
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame= CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [window addSubview:self];
    [self requestItemList:specId];
    
}
-(void)requestItemList:(NSString *)specId
{
    [noDataView removeFromSuperview];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    
    if (Token ==nil) {
        Token=@"dd";
    }
    
    [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:30 view:self ];
    NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
    [params setObject:Token forKey:@"tokenId"];
    
    [params setObject:specId forKey:@"specId"];
    
    [HttpRequest requestData:@"Product/Inventory/Company" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"severDataDict=====%@",severDataDict.dataDict);
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self ];

                StockManagementPopModel *dataModel=[StockManagementPopModel dataModelWithDict:[severDataDict.dataDict objectForKey:@"currentSpec"]];
                [self setPopTop:dataModel];
                
                NSMutableArray * mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"companyStocks"]];
                
                if (mItemsArray.count==0) {
                    noDataView=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-50, ScreenWidth, 80)];
                    noDataView.center=parentView.center;
                    [parentView addSubview:noDataView];
                    UIImageView*noImage=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-47, -30, 94, 51)];
                    noImage.image=[UIImage imageNamed:@"meiyoushuju"];
                    [noDataView addSubview:noImage];
                    UILabel*noLabel1=[[UILabel alloc]initWithFrame:CGRectMake(0,35, ScreenWidth, 14)];
                    noLabel1.text=@"没有列表数据，赶紧去添加！";
                    noLabel1.textColor=zhonghuiColor; noLabel1.font=[UIFont systemFontOfSize:14];
                    noLabel1.textAlignment=NSTextAlignmentCenter;
                    [noDataView addSubview:noLabel1];

                }
                for (NSDictionary *dic in mItemsArray) {
                    StockManagementPopListModel *dataModel=[StockManagementPopListModel dataModelWithDict:dic];
                    [cellModelArray addObject:dataModel];
                    
                }
                [listTableView reloadData];
                
            }
            else
            {
                [Global hideProgressHUDInternal:self];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                //  [Global showTextHud:requestResultStr view:self.view];
                [Global showTextHudSub:requestResultStr view:self];
//                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
//                    
//                    TheLoginViewController *loginVC=[[TheLoginViewController alloc] init];//2.0{9个参数 少2个}
//                    [self.navigationController pushViewController:loginVC animated:YES];
//                }
                
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self];
            [Global showTextHudSub:severDataDict.resultStr view:self];
        }
    }];
    
}
-(void)setPopTop:(StockManagementPopModel *)model
{
     productStockLabel.text=[NSString stringWithFormat:@"库存 %d",[model.specInv intValue]];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"库存 %d",[model.specInv intValue]]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:12.0]
     
                          range:NSMakeRange(0, 2)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:zhonghuiColor
     
                          range:NSMakeRange(0, 2)];
    productStockLabel.attributedText = AttributedStr;
    
    productSpecLabel.text=[NSString stringWithFormat:@"%@ %@",model.colourName,model.sizeName];
//    [productImageView setImageWithURL:[NSURL URLWithString:model.imageUrl]];
      [productImageView setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@"zhengfangxing"]];
    [productNameLabel setText:model.itemName];

}
#pragma mark - tableView代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return cellModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const  CellIdentifier = @"StockManagementPopCell";
    StockManagementPopCell *cell=( StockManagementPopCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"StockManagementPopCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model=cellModelArray[indexPath.row];
    return cell;
    
}

-(void)setAutoLayout
{
    [topAlphaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@74);
        
    }];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topAlphaBtn.mas_bottom);
        make.width.equalTo(@80);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@40);
        
    }];
    [parentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topAlphaBtn.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
        
    }];
    [productImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(parentView.mas_top).with.offset(24);
        make.left.equalTo(parentView.mas_left).with.offset(10);
        make.width.equalTo(@82);
        make.height.equalTo(@82);
        
    }];
    [productNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(productImageView.mas_top) ;
        make.left.equalTo(productImageView.mas_right).with.offset(12);
        make.right.equalTo(self.mas_right).with.offset(-8);
        make.bottom.equalTo(productSpecLabel.mas_top).with.offset(-3);
        
    }];
    
    [productSpecLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(productNameLabel.mas_bottom).with.offset(3) ;
        make.left.equalTo(productNameLabel.mas_left) ;
        make.right.equalTo(self.mas_right).with.offset(-8);
        make.height.equalTo(@13);
        
    }];
    [productStockLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(productImageView.mas_bottom) ;
        make.left.equalTo(productNameLabel.mas_left) ;
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@18);
        
    }];
    
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(productImageView.mas_bottom).with.offset(20);
        make.left.equalTo(parentView.mas_left).with.offset(10);
        make.right.equalTo(parentView.mas_right).with.offset(-10);
        make.height.equalTo(@0.5);
        
    }];
    [listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineLabel.mas_bottom)  ;
        make.left.equalTo(parentView.mas_left);
        make.right.equalTo(parentView.mas_right);
        make.bottom.equalTo(parentView.mas_bottom);
    }];
    
    
}

@end
